package sys.knot.configuration;

import sys.knot.data.access.NamedDataSource;
import sys.knot.interpreter.ResultSetInterpreter;
import sys.knot.model.QueryParameter;

import java.util.List;
import java.util.Map;

public class RawConfig {
    public RawConfig() {}

    public String method;
    public String url;
    public int limitRows = 0;
    public List<InputValidator> indataValidators;
    public List<Query> queries;

    public void validate() throws ConfigurationException {
        checkRequired(method,    "method");
        checkRequired(url,       "url");
        checkRequired(limitRows, "limitRows");
        checkRequired(queries,   "queries");

        for (Query q : queries) {
            checkRequired(q.queryName,         "queryName");
            checkRequired(q.sql,               "sql");
            checkRequired(q.resultInterpreter, "resultInterpreter");
            checkRequired(q.datasources,       "datasources");
        }
    }
    private void checkRequired(String parameter, String parameterName) throws ConfigurationException {
        if (parameter == null || parameter.length() == 0) {
            throw new ConfigurationException("Missing of invalid required parameter: " + parameterName + " = " + parameter);
        }
    }
    private void checkRequired(List parameter, String parameterName) throws ConfigurationException {
        if (parameter == null || parameter.size() == 0) {
            throw new ConfigurationException("Missing of invalid required parameter: " + parameterName + " = " + parameter);
        }
    }
    private void checkRequired(int parameter, String parameterName) throws ConfigurationException {
        if (parameter <= 0) {
            throw new ConfigurationException("Missing of invalid required parameter: " + parameterName + " = " + parameter);
        }
    }

    public static class InputValidator {
        public InputValidator() {}

        public String parameter;
        public List<String> validators;
    }

    public static class Query {
        public Query() {}

        public String queryName;
        public String sql;
        public String resultInterpreter;
        public List<String> datasources;
    }
}
